package com.wgzx.modbus.message;

import com.wgzx.modbus.entity.ModbusRsp;
import com.wgzx.modbus.entity.ModbusRspFrame;
import com.wgzx.modbus.entity.exception.ConnectionException;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;

/**
 * 消息管理器 </br>
 * 负责将请求与响应配对 </br>
 *
 * @author gexin
 */
public interface MsgManager {

    /**
     * 异步获取响应
     *
     * @param transactionId 事务标识符
     * @return Mono<T extends ModbusRsp>
     */
    <T extends ModbusRsp> Mono<T> getResponseAsync(int transactionId, Callback callback) throws ConnectionException;

    /**
     * 同步获取响应
     *
     * @param transactionId 事务标识符
     * @return CompletableFuture<T extends ModbusRsp>
     */
    <T extends ModbusRsp> CompletableFuture<T> getResponseSync(int transactionId);

    /**
     * 响应到达
     *
     * @param transactionId 事务标识符
     * @param responseFrame modbus响应消息
     */
    void responseArrive(int transactionId, ModbusRspFrame<?> responseFrame);

    /**
     * 响应超时
     *
     * @param transactionId 事务标识符
     */
    void responseTimeout(int transactionId);

}
